var _excluded = ['options', 'fieldNames', 'authedBy', 'value', 'onChange'];
function _extends() {
  _extends = Object.assign
    ? Object.assign.bind()
    : function (target) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];
          for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }
        return target;
      };
  return _extends.apply(this, arguments);
}
function _toConsumableArray(arr) {
  return (
    _arrayWithoutHoles(arr) ||
    _iterableToArray(arr) ||
    _unsupportedIterableToArray(arr) ||
    _nonIterableSpread()
  );
}
function _nonIterableSpread() {
  throw new TypeError(
    'Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.',
  );
}
function _unsupportedIterableToArray(o, minLen) {
  if (!o) return;
  if (typeof o === 'string') return _arrayLikeToArray(o, minLen);
  var n = Object.prototype.toString.call(o).slice(8, -1);
  if (n === 'Object' && o.constructor) n = o.constructor.name;
  if (n === 'Map' || n === 'Set') return Array.from(o);
  if (n === 'Arguments' || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))
    return _arrayLikeToArray(o, minLen);
}
function _iterableToArray(iter) {
  if (
    (typeof Symbol !== 'undefined' && iter[Symbol.iterator] != null) ||
    iter['@@iterator'] != null
  )
    return Array.from(iter);
}
function _arrayWithoutHoles(arr) {
  if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}
function _arrayLikeToArray(arr, len) {
  if (len == null || len > arr.length) len = arr.length;
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  return arr2;
}
function _objectWithoutProperties(source, excluded) {
  if (source == null) return {};
  var target = _objectWithoutPropertiesLoose(source, excluded);
  var key, i;
  if (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }
  return target;
}
function _objectWithoutPropertiesLoose(source, excluded) {
  if (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;
  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }
  return target;
}
import { Cascader } from 'antd';
import React, { Fragment, useCallback, useMemo } from 'react';
import { useFieldContext } from '../../_utils/contexts/FieldContext';
import useRequestOptions from '../../_utils/hooks/useRequestOptions';
import createField from '../../createField';
var AuthedCascader = function AuthedCascader(_ref) {
  var optionsProps = _ref.options,
    fieldNames = _ref.fieldNames,
    authedBy = _ref.authedBy,
    value = _ref.value,
    onChange = _ref.onChange,
    props = _objectWithoutProperties(_ref, _excluded);
  var _ref2 = useFieldContext(),
    label = _ref2.label;
  var _ref3 = fieldNames || {},
    _ref3$value = _ref3.value,
    valueKey = _ref3$value === void 0 ? 'value' : _ref3$value,
    _ref3$label = _ref3.label,
    labelKey = _ref3$label === void 0 ? 'label' : _ref3$label,
    _ref3$children = _ref3.children,
    childrenKey = _ref3$children === void 0 ? 'children' : _ref3$children;
  var _useRequestOptions = useRequestOptions(optionsProps),
    options = _useRequestOptions.options;
  var innerOptions = useMemo(
    function () {
      if (authedBy === true)
        return {
          readonlyOptions: [],
          configOptions: options,
        };
      var readonlyOptions = [];
      var configOptions = [];
      var traverse = function traverse(opts) {
        var level = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
        var matched = opts.find(function (opt) {
          return authedBy && opt[valueKey] === authedBy[level];
        });
        if (!matched) return;
        if (level + 1 === (authedBy === null || authedBy === void 0 ? void 0 : authedBy.length)) {
          configOptions = matched[childrenKey];
        }
        if (level < authedBy.length) {
          readonlyOptions.push(matched);
          if (matched.children) traverse(matched.children, level + 1);
        }
      };

      // @ts-ignore
      traverse(options);
      return {
        readonlyOptions: readonlyOptions,
        configOptions: configOptions,
      };
    },
    [options, authedBy],
  );
  var readonlyValues = useMemo(
    function () {
      var readonlyOptions = innerOptions.readonlyOptions;
      return readonlyOptions.map(function (item) {
        return item[valueKey];
      });
    },
    [innerOptions],
  );
  var readonlyLabels = useMemo(
    function () {
      var readonlyOptions = innerOptions.readonlyOptions;
      return readonlyOptions.map(function (item) {
        return item[labelKey];
      });
    },
    [innerOptions],
  );
  var configOptions = useMemo(
    function () {
      return innerOptions.configOptions;
    },
    [innerOptions],
  );
  var handleChange = useCallback(
    function (value) {
      if (onChange)
        if (value) {
          // @ts-expect-error
          onChange(
            !readonlyValues.length
              ? value
              : [].concat(_toConsumableArray(readonlyValues), _toConsumableArray(value)),
          );
        } else {
          // @ts-expect-error
          onChange(!readonlyValues.length ? value : _toConsumableArray(readonlyValues));
        }
    },
    [readonlyValues, onChange],
  );
  return /*#__PURE__*/ React.createElement(
    Fragment,
    null,
    readonlyLabels.length
      ? /*#__PURE__*/ React.createElement('span', null, readonlyLabels.join(''))
      : '',
    configOptions.length
      ? /*#__PURE__*/ React.createElement(
          Cascader,
          _extends(
            {
              options: configOptions,
              placeholder: '\u8BF7\u9009\u62E9'.concat(label || ''),
              style: !readonlyLabels.length
                ? undefined
                : {
                    width: '72%',
                    marginLeft: 5,
                  },
            },
            props,
            {
              value:
                value !== undefined
                  ? value.slice(!readonlyValues.length ? 0 : readonlyValues.length)
                  : undefined,
              onChange: handleChange,
            },
          ),
        )
      : null,
  );
};
export default createField(AuthedCascader, {
  defaultDetailType: 'cascader',
});
